<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node734.html" />
<link rel="prev" href="node732.html" />
<link rel="parent" href="node732.html" />
<link rel="next" href="node734.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>21.1.2.1 The NullTranslations class</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="21.1.2 class-based API"
  href="node732.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="21.1.2 class-based API"
  href="node732.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="21.1.2.2 the GNUTranslations class"
  href="node734.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node732.html">21.1.2 Class-based API</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node732.html">21.1.2 Class-based API</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node734.html">21.1.2.2 The GNUTranslations class</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION0023121000000000000000">
21.1.2.1 The <tt class="class">NullTranslations</tt> class</a>
</h3>
Translation classes are what actually implement the translation of
original source file message strings to translated message strings.
The base class used by all translation classes is
<tt class="class">NullTranslations</tt>; this provides the basic interface you can use
to write your own specialized translation classes.  Here are the
methods of <tt class="class">NullTranslations</tt>:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4817' xml:id='l2h-4817' class="method">__init__</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>fp</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Takes an optional file object <var>fp</var>, which is ignored by the base
class.  Initializes ``protected'' instance variables <var>_info</var> and
<var>_charset</var> which are set by derived classes, as well as <var>_fallback</var>,
which is set through <tt class="method">add_fallback</tt>.  It then calls
<code>self._parse(fp)</code> if <var>fp</var> is not <code>None</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4818' xml:id='l2h-4818' class="method">_parse</tt></b>(</nobr></td>
  <td><var>fp</var>)</td></tr></table></dt>
<dd>
No-op'd in the base class, this method takes file object <var>fp</var>, and
reads the data from the file, initializing its message catalog.  If
you have an unsupported message catalog file format, you should
override this method to parse your format.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4819' xml:id='l2h-4819' class="method">add_fallback</tt></b>(</nobr></td>
  <td><var>fallback</var>)</td></tr></table></dt>
<dd>
Add <var>fallback</var> as the fallback object for the current translation
object. A translation object should consult the fallback if it cannot
provide a translation for a given message.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4820' xml:id='l2h-4820' class="method">gettext</tt></b>(</nobr></td>
  <td><var>message</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">gettext()</tt> to the fallback.
Otherwise, return the translated message.  Overridden in derived classes.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4821' xml:id='l2h-4821' class="method">lgettext</tt></b>(</nobr></td>
  <td><var>message</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">lgettext()</tt> to the fallback.
Otherwise, return the translated message.  Overridden in derived classes.

<p>

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4822' xml:id='l2h-4822' class="method">ugettext</tt></b>(</nobr></td>
  <td><var>message</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">ugettext()</tt> to the fallback.
Otherwise, return the translated message as a Unicode string.
Overridden in derived classes.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4823' xml:id='l2h-4823' class="method">ngettext</tt></b>(</nobr></td>
  <td><var>singular, plural, n</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">ngettext()</tt> to the fallback.
Otherwise, return the translated message.  Overridden in derived classes.

<p>

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4824' xml:id='l2h-4824' class="method">lngettext</tt></b>(</nobr></td>
  <td><var>singular, plural, n</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">ngettext()</tt> to the fallback.
Otherwise, return the translated message.  Overridden in derived classes.

<p>

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4825' xml:id='l2h-4825' class="method">ungettext</tt></b>(</nobr></td>
  <td><var>singular, plural, n</var>)</td></tr></table></dt>
<dd>
If a fallback has been set, forward <tt class="method">ungettext()</tt> to the fallback.
Otherwise, return the translated message as a Unicode string.
Overridden in derived classes.

<p>

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4826' xml:id='l2h-4826' class="method">info</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the ``protected'' <tt class="member">_info</tt> variable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4827' xml:id='l2h-4827' class="method">charset</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the ``protected'' <tt class="member">_charset</tt> variable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4828' xml:id='l2h-4828' class="method">output_charset</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the ``protected'' <tt class="member">_output_charset</tt> variable, which
defines the encoding used to return translated messages.

<p>

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4829' xml:id='l2h-4829' class="method">set_output_charset</tt></b>(</nobr></td>
  <td><var>charset</var>)</td></tr></table></dt>
<dd>
Change the ``protected'' <tt class="member">_output_charset</tt> variable, which
defines the encoding used to return translated messages.

<p>

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4830' xml:id='l2h-4830' class="method">install</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>unicode
                                              </var><big>[</big><var>, names</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
If the <var>unicode</var> flag is false, this method installs
<tt class="method">self.gettext()</tt> into the built-in namespace, binding it to
"<tt class="samp">_</tt>".  If <var>unicode</var> is true, it binds <tt class="method">self.ugettext()</tt>
instead.  By default, <var>unicode</var> is false.

<p>
If the <var>names</var> parameter is given, it must be a sequence containing
the names of functions you want to install in the builtin namespace in
addition to <tt class="function">_()</tt>. Supported names are <code>'gettext'</code> (bound
to <tt class="method">self.gettext()</tt> or <tt class="method">self.ugettext()</tt> according to the
<var>unicode</var> flag), <code>'ngettext'</code> (bound to <tt class="method">self.ngettext()</tt>
or <tt class="method">self.ungettext()</tt> according to the <var>unicode</var> flag),
<code>'lgettext'</code> and <code>'lngettext'</code>.

<p>
Note that this is only one way, albeit the most convenient way, to
make the <tt class="function">_</tt> function available to your application.  Because it
affects the entire application globally, and specifically the built-in
namespace, localized modules should never install <tt class="function">_</tt>.
Instead, they should use this code to make <tt class="function">_</tt> available to
their module:

<p>
<div class="verbatim"><pre>
import gettext
t = gettext.translation('mymodule', ...)
_ = t.gettext
</pre></div>

<p>
This puts <tt class="function">_</tt> only in the module's global namespace and so
only affects calls within this module.

<p>

<span class="versionnote">Changed in version 2.5:
Added the <var>names</var> parameter.</span>

</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="21.1.2 class-based API"
  href="node732.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="21.1.2 class-based API"
  href="node732.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="21.1.2.2 the GNUTranslations class"
  href="node734.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node732.html">21.1.2 Class-based API</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node732.html">21.1.2 Class-based API</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node734.html">21.1.2.2 The GNUTranslations class</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
